<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>修改用户</title>
    <script src="js/vue.min.js"></script>
    <script src="js/axios.min.js"></script>
</head>
<body>
<div id="app">
    <table border="1" style="margin: 0 auto;width: 300px">

        <tr>
            <th colspan="2">修改用户</th>
        </tr>
        <tr>
            <td>姓名：</td>
            <td>
                <!--隐藏域-->
                <input type="hidden" v-model="user.id">
                <input v-model="user.name" type="text">
            </td>
        </tr>
        <tr>
            <td>年龄：</td>
            <td><input v-model="user.age" type="text"></td>
        </tr>
        <tr>
            <td colspan="2">
                <button @click="updateUser" type="button">修改</button>
            </td>
        </tr>
    </table>

</div>

<script>
    //创建vue
    var vue = new Vue({
        el: "#app",
        data: {
            user: {
                id:"",
                name: "",
                age: ""
            }
        },
        //在页面加载渲染完毕以后执行内部代码
        mounted(){
          //发送请求根据id查询单个用户信息
          axios.get("/user/getById?id="+getUrlId()).then(function (res){
              //获取返回结果,并将返回结果设置给vue对象
              vue.user = res.data.data;
          });
        },
        methods: {
            updateUser: function () {
                //发送请求，提交数据给后端
                axios.post("/user/update",vue.user).then(function (res){
                    if(res.data.code===200){
                        alert("修改用户成功");
                        //刷新页面
                        location.reload();
                    }else{
                        alert(res.data.message);
                    }

                })
            }
        }
    })
    //获取地址栏中参数id值
    function getUrlId(){
        //获取url地址参数 ?id=123
        var search = location.search;
        //切割后获取id的值
        var id =  search.split("=")[1];
        return id;
    }

</script>
</body>
</html>
